package chuichui.manager
{
	import chuichui.Utils.Print;
	import chuichui.Utils.Val;
	import chuichui.game.avatar.Animation;
	import chuichui.game.avatar.Avatar;
	import chuichui.game.avatar.Frame;
	import chuichui.resource.Resources;

	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.system.ApplicationDomain;
	import flash.utils.Dictionary;

	/**
	 * 动作，特效管理
	 * @author yangyang
	 *
	 */
	public class AnimationManager
	{
		private static var instance : AnimationManager;

		private var animtionsData : Dictionary = new Dictionary();
		private var effectAnimations : Dictionary = new Dictionary();

		private var resources : Resources;

		public function AnimationManager()
		{
			resources = Resources.getInstance();
		}


		/**
		 * 单列
		 * @return
		 *
		 */
		public static function getInstance() : AnimationManager
		{
			if(instance == null)
			{
				instance = new AnimationManager();
			}
			return instance;
		}

		public function createAvatar(id : String, weaponId : String) : Avatar
		{
			var avatar : Avatar = new Avatar(id, getAvatarAnimation(id, weaponId))
			return avatar;
		}

		/**
		 * 创建人物的动作动画
		 * @param id
		 * @param weaponId
		 * @return
		 *
		 */
		public function createAvatarAnimation(id : String, weaponId : String) : Dictionary
		{
			var actionClass : Class;
			var animation : Animation;
			var animationMovie : MovieClip;
			var actionDirAnimation : Dictionary = new Dictionary();
			var actionType : String;
			var animationId : String;
			var action_types : Array = ["body", "weapon", "other"];

			for(var i : int = 0; i <= 9; i++)
			{
				var animations : Array = [];

				if(i == Val.DEAD_AVATAR)
				{
					action_types = ["weapon", "body", "other"];
				}
				else
				{
					action_types = ["body", "weapon", "other"];
				}

				for each(actionType in action_types)
				{
					if(actionType == "weapon")
					{
						animationId = actionType + "." + weaponId + "." + i;
					}
					else
					{
						animationId = actionType + "." + id + "." + i;
					}

					actionClass = resources.getClass(animationId);

					if(actionClass == null)
					{
						continue;
					}
					animationMovie = new actionClass() as MovieClip;
					animation = new Animation();
					animation.name = actionType;
					animation.createAnimationFrames(animationMovie);
					animations.push(animation);
				}
				actionDirAnimation[i] = animations;
			}
			animtionsData[id + "," + weaponId] = actionDirAnimation;
			return animtionsData;
		}

		/**
		 * 合成动作Frame
		 * @param index
		 * @return
		 *
		 */
		private function composeAnimations(id : String, animations : Array, duration : int) : Array
		{
			var centerX : int = 200;
			var centerY : int = 200;

			var bd : BitmapData = new BitmapData(400, 400, true, 0);
			var boundRect : Rectangle = new Rectangle();
			var point : Point = new Point();
			var frame : Frame;
			var frames : Array = [];

			if(animations.length == 0)
			{
				return null;
			}

			var len : int = animations[0].frameCount;

			for(var i : int = 0; i < len; i++)
			{
				boundRect.left = int.MAX_VALUE;
				boundRect.top = int.MAX_VALUE;
				boundRect.bottom = int.MIN_VALUE;
				boundRect.right = int.MIN_VALUE;

				bd.fillRect(bd.rect, 0);

				for each(var animation : Animation in animations)
				{
					frame = animation.getFrame(i);

					if(frame == null || frame.data == null)
					{
						continue;
					}
					point.x = centerX + frame.offsetX;
					point.y = centerY + frame.offsetY;
					//计算该合成帧的最小包围框
					bound(boundRect, point.x, point.y, frame.data.width, frame.data.height);
					bd.copyPixels(frame.data, frame.data.rect, point, null, null, true);
				}
				var bmd : BitmapData = new BitmapData(boundRect.width, boundRect.height, true, 0);
				point.x = 0;
				point.y = 0;
				bmd.copyPixels(bd, boundRect, point, null, null, true);

				var composdFrame : Frame = new Frame();
				composdFrame.data = bmd;
				composdFrame.offsetX = boundRect.x - centerX;
				composdFrame.offsetY = boundRect.y - centerY;
				composdFrame.duration = duration;
				frames.push(composdFrame);
			}
			bd.dispose();

			return frames;
		}

		private function bound(dest : Rectangle, left : int, top : int, width : int, height : int) : void
		{
			var right : int = left + width;
			var bottom : int = top + height;

			if(left < dest.left)
			{
				dest.left = left;
			}

			if(top < dest.top)
			{
				dest.top = top;
			}

			if(right > dest.right)
			{
				dest.right = right;
			}

			if(bottom > dest.bottom)
			{
				dest.bottom = bottom;
			}
		}

		/**
		 * 创建怪物avatar
		 * @param id
		 * @return
		 *
		 */
		public function createMonsterAvatarAnimation(id : String) : Dictionary
		{
			var animation : Animation;
			var actionDirAnimation : Dictionary = new Dictionary();
			var animationId : String;

			var actionIndex : Object = {0: 0, 1: 1, 2: 3, 6: 4, 7: 2};

			for(var i : int = 0; i <= 7; i++)
			{
				//死亡动画特殊处理
				if(i == Val.MONSTER_AVATAR_DEAD)
				{
					continue;
				}
				else
				{
					animationId = "monster." + id + "." + i;
				}

				animation = createAnimationByClassName(animationId);

				if(animation == null)
				{
					continue;
				}
				animation.name = "body";
				actionDirAnimation[actionIndex[i]] = [animation];
			}
			animtionsData[id + ","] = actionDirAnimation;
			return animtionsData;
		}

		private function createAnimationByClassName(className : String) : Animation
		{
			var actionClass : Class = resources.getClass(className);
			var animationMovie : MovieClip;

			if(actionClass == null)
			{
				return null;
			}

			animationMovie = new actionClass() as MovieClip;
			var animation : Animation = new Animation();
			animation.createAnimationFrames(animationMovie);
			return animation;
		}

		/**
		 * 创建特效动画
		 * @param id
		 * @return
		 *
		 */
		public function createEffectAnimation(id : String) : Animation
		{
			Print.warning("生成特效:", id);
			var animation : Animation = new Animation();
			var actionClass : Class = resources.getClass(id);

			if(actionClass == null)
			{
				return null;
			}
			var effectMovie : MovieClip = new actionClass() as MovieClip;
			animation.createAnimationFrames(effectMovie, 1000 / 12);
			effectAnimations[id] = animation;
			return animation;
		}

		/**
		 * 获得人物动作动画
		 * @param id
		 * @return
		 *
		 */
		public function getAvatarAnimation(id : String, weaponId : String) : Dictionary
		{
			return animtionsData[id + "," + weaponId];
		}

		/**
		 * 获得特效动画
		 * @param id
		 * @return
		 *
		 */
		public function getEffectiAnimation(id : String) : Animation
		{
			var animation : Animation = effectAnimations[id];

			if(animation == null)
			{
				animation = createEffectAnimation(id);

				if(animation == null)
				{
					return null;
				}
				return animation.clone();
			}
			return animation.clone();
		}

		/**
		 * 清理掉动作动画
		 *
		 */
		public function clearAnimation() : void
		{
			var actionList : Array;
			var animation : Animation;

			for(var key : String in animtionsData)
			{
				var data : Dictionary = animtionsData[key];

				for(var key1 : String in data)
				{
					actionList = data[key1];

					for each(animation in actionList)
					{
						animation.destroy(true);
					}
					actionList.length = 0;
					delete data[key1];
				}
				delete animtionsData[key];
			}
		}

		/**
		 * 清理掉特效动画。
		 *
		 */
		public function clearEffect() : void
		{
			for(var key : String in effectAnimations)
			{
				var effect : Animation = effectAnimations[key];
				effect.destroy(true);
				delete effectAnimations[key];
			}
		}
	}
}